package com.itheima.config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

//四个配置类代替四个配置文件
@Configuration/*申明配置类*/
@PropertySource("classpath:jdbc.properties")
public class MybatisConfig {
    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    @Bean("dataSource")/*用@Bean 注解表明getDatasource需要交给Spring进行管理*/
    public DataSource getDatasource() throws PropertyVetoException {
        ComboPooledDataSource datasource = new ComboPooledDataSource();
        datasource.setDriverClass(driver);
        datasource.setPassword(password);
        datasource.setUser(username);
        datasource.setJdbcUrl(url);
        return datasource;
    }


    //todo：因为需要Spring整合，所以把Mybatis的这个两个配置放到spring配置文件中，放在mybatis中可能会解析不到
    //其实都写到spring里面都是可以的，前提是这两个必须在spring里面

    //@Bean
    //public SqlSessionFactoryBean getSqlSessionFactoryBean(@Qualifier("dataSource") DataSource dataSource) {
    //    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    //    factoryBean.setDataSource(dataSource);
    //    factoryBean.setTypeAliasesPackage("com.itheima.pojo");
    //    return factoryBean;
    //}
    //
    //@Bean
    //public MapperScannerConfigurer getMapperScannerConfigurer() {
    //    MapperScannerConfigurer configurer = new MapperScannerConfigurer();
    //    configurer.setBasePackage("com.itheima.dao");
    //    return configurer;
    //}


}
